<?php

namespace app\common\model;

use app\common\exception\BusinessException;

class User extends Base
{

    protected $autoWriteTimestamp = false;

    /**
     * getUserInfo
     * @param $uid
     * @param $field
     * @return array
     * @throws
     */
    public function getUserInfo($uid, $field = '*'): array
    {
        $userInfo = $this->where(['id' => $uid])->field($field)->find();
        return $userInfo ? $userInfo->toArray() : [];
    }

    /**
     * getUserValue
     * @param $uid
     * @param $name
     * @param $value
     * @return int
     */
    public function getUserValue($uid, $name, $value)
    {
        return $this->where(['id' => $uid])->value($name, $value);
    }

    /**
     * checkIsOpenPlatformUser
     * @param $uid
     * @return bool
     */
    public function checkIsOpenPlatformUser($uid): bool
    {
        return $this->getUserValue($uid, 'is_open_platform', 0);
    }

    /**
     * isAgent
     * @param $uid
     * @return bool
     */
    public function isAgent($uid): bool
    {
        return $this->getUserValue($uid, 'is_agent', 0) == 1;
    }

    /**
     * getUserRanking
     * @return array
     */
    public function getUserRanking(): array
    {
        // 提交查询
        $field = "uid,number_of_answers as count";

        // 查询数据
        $where = [
            ['update_time', '>', strtotime("-1 month")]
        ];
        $list = (new UserExtend())->field($field)->where($where)->order(
            'number_of_answers',
            'desc'
        )->limit(10)->select()->toArray();

        // 填充数据
        $userIds = array_column($list, 'uid');
        if ($userIds) {
            $usersList = $this->where([['id', 'in', $userIds]])->column('id,nickname,avatar,region_id', 'id');
            $region_ids = array_column($usersList, 'region_id');
            if ($region_ids) {
                $regionList = Region::where([['id', 'in', $region_ids]])->column('id,name', 'id');
            }
        }
        foreach ($list as $key => $val) {
            // 提取参数
            $userId = $val['uid'];
            $region_id = $usersList[$userId]['region_id'] ?? 0;
            $list[$key]['nickname'] = $usersList[$userId]['nickname'] ?? '-';
            $list[$key]['avatar'] = $usersList[$userId]['avatar'] ?? '';
            $list[$key]['avatar'] = fillUserAvatar($list[$key]['avatar']);
            $list[$key]['region_name'] = $regionList[$region_id]['name'] ?? '-';

            // 移除参数
            unset($list[$key]['user_id']);
        }

        // 返回
        return $list;
    }

    /**
     * checkIsExistByUid
     * @param $uid
     * @return bool
     */
    public function checkIsExistByUid($uid): bool
    {
        return (bool)$this->where(['id' => $uid])->count();
    }

    /**
     * 查询用户的专业信息
     * @param $uid
     * @return mixed
     * @throws BusinessException
     */
    public function getUserProfession($uid)
    {
        // 查询用户信息
        $userInfo = $this->getUserInfo($uid, 'id,profession_id');
        if (!$userInfo) {
            throw new BusinessException('用户信息不存在');
        }
        if (!$userInfo['profession_id']) {
            return false;
        }

        // 查询专业信息
        return app(Profession::class)->getInfo($userInfo['profession_id'], '*');
    }

    /**
     * getUserAvatarForDisplay
     * @param $count
     * @return array
     */
    public function getUserAvatarForDisplay($count)
    {
        // 查询条件
        $where = [
            ['openid', '<>', '']
        ];

        // 排序方式
        $order = ['id' => 'desc'];

        // 查询数据
        $list = $this->where($where)->order($order)->column('avatar');

        // 返回
        return $list ?: [];
    }

}